/*
 * Dijjer - A Peer to Peer HTTP Cache
 * Copyright (C) 2004,2005 Change.Tv, Inc, Mathias De Maré
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package dijjer.io.comm;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

import dijjer.util.VeryLongInteger;
import dijjer.util.logging.Logger;

public class WebPeer extends AbstractPeer {
	InetAddress address;
	int port;
	
	public WebPeer() throws Exception {
		this(InetAddress.getLocalHost(),0);
	}
	public WebPeer(DataInputStream dis) throws Exception {
		
		byte[] ba = new byte[4]; //TODO: not ipv6-proof!
		
		dis.read(ba);
		
//		this.ad = new WebDestination(InetAddress.getByAddress(ba).getHostAddress()+":"+dis.readInt());
		address = InetAddress.getByAddress(ba);
		port = dis.readInt();
		this.hash = new VeryLongInteger(toString());
	}
	
	public WebPeer(InetAddress address, int port) throws Exception {
		this.address = address;
		this.port = port;
		this.hash = new VeryLongInteger(toString());
	}
	
	public WebPeer(String destString) throws Exception {
		StringTokenizer st = new StringTokenizer(destString, ":");
		if(destString.charAt(0) >= '0' && destString.charAt(0) <= '9')
			this.address = InetAddress.getByName((String) st.nextElement());
		else //TODO: find reason for this madness!
			this.address = InetAddress.getByName(((String) st.nextElement()).substring(1));
		port = Integer.parseInt((String) st.nextElement());
		this.hash = new VeryLongInteger(toString());
	}
	
	public VeryLongInteger getHash() {
		return hash;
	}
	
	public int hashCode() {
		return address.hashCode() + port;
	}
	
	public boolean equals(Object o) {
		if(!(o instanceof WebPeer))
			return false;
		WebPeer peer = (WebPeer) o;
		if(!(peer.address.equals(address)))
			return false;
		if(!(peer.port == port))
			return false;
		if (!(peer.hash.equals(hash)))
			return false;
		return true;
	}
	
	public String toString() {
		return (address != null ? address.getHostName() : "null") + ":" + port;
	}
	
	public void writeToDataOutputStream(DataOutputStream dos) throws IOException {
		dos.write(address.getAddress());
		dos.writeInt(port);
	}
	
	public boolean isNull() {
		if(address == null || port == 0)
			return true;
		return false;
	}
	
	public InetAddress getAddress() {
		return address;
	}
	
	public int getPort() {
		return port;
	}
}
